#include "linux_def.h"
#include "core.h"
#include "event.h"

ssize_t g_send(connection_t *c, char *buf, size_t size)
{
	ssize_t	n;
	event_t	*wev;

	wev = c->write;


	for ( ;; ) {
		n = send(c->fd, buf, size, 0);

		if (n > 0){
			if(n < (ssize_t) size){
				wev->ready = 0;
			}
			printf("%s, %d, %s\n", __FILE__, __LINE__, __func__);
			return n;
		}

        	if (n == 0) {
			printf("%s, %d, %s\n", __FILE__, __LINE__, __func__);
            		wev->ready = 0;
            		return n;
        	}

		if (errno == EAGAIN || errno == EINTR) {
			printf("%s, %d, %s\n", __FILE__, __LINE__, __func__);
			wev->ready = 0;
			if (errno == EAGAIN) {
				return EAGAIN;
			}
		} else {
			printf("%s, %d, %s\n", __FILE__, __LINE__, __func__);
			return errno;
		}
	}

	wev->ready = 0;

	if (verbose)
		printf("%s, %d, %s\n", __FILE__, __LINE__, __func__);
}
